/*
 * gbdt_feature.cc
 *
 *  Created on: 2012-7-21
 *      Author: wanghaox
 */
#include "gbdt_feature.h"

GbdtFeature::GbdtFeature() {
}

GbdtFeature::~GbdtFeature() {
	// TODO
}

void GbdtFeature::set(int instance, float value) {
	_raw_features[instance] = value;
//	show("%d:%f\t", instance, value);
}

bool GbdtFeature::init(int instance_num, int feature_index) {
	_global_feature_index = feature_index;
	_instance_num = instance_num;
	_raw_features = new double[_instance_num];
	_sored_feature = new double[_instance_num];
	_sorted_instance = new int[_instance_num];

	_instance_to_treenode = new int[_instance_num];

	memset(_raw_features, 0, _instance_num*sizeof(_raw_features));
	memset(_sored_feature, 0, _instance_num*sizeof(_sored_feature));
	memset(_sorted_instance, 0, _instance_num*sizeof(_sorted_instance));
	memset(_instance_to_treenode, 0, _instance_num*sizeof(_instance_to_treenode));

	return true;
}

void GbdtFeature::reset_instance_node() {
	// clear nodes before next tree
	memset(_instance_to_treenode, 0, _instance_num * sizeof(int));
}

//struct CompareFeatureValuePairs {
//	bool operator()(FeatureValuePair* fv1, FeatureValuePair* fv2) {
//		return (fv1->value < fv2->value);
//	}
//};

void GbdtFeature::sort_instance_by_value() {
//	// initialize FeatureValue array
//	CompareFeatureValuePairs cfvp;
//	FeatureValuePair** pairs = new FeatureValuePair*[N];
//	for (int i = 0; i < N; i++)
//		pairs[i] = new FeatureValuePair();
//
//	// sort each feature
//	for (int f = 0; f < numfeatures; f++) {
//		// load feature into pairs array
//		for (int i = 0; i < N; i++) {
//			pairs[i]->index = i;
//			pairs[i]->value = rawfeatures[f][i];
//		}
//
//		// sort pairs array
//		std::sort(pairs, pairs + N, cfvp);
//
//		// write feature to sortedfeatures, sortedindices
//		for (int i = 0; i < N; i++) {
//			sortedfeatures[f][i] = pairs[i]->value;
//			sortedindices[f][i] = pairs[i]->index;
//		}
//	}
//
//	// delete FeatureValue array
//	for (int i = 0; i < N; i++) {
//		delete pairs[i];
//		pairs[i] = NULL;
//	}
//	delete[] pairs;
}

